package com.xiaoq.users.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.xiaoq.apis.user.*;
import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.social.entity.FollowEntity;
import com.xiaoq.social.repository.IFollowRepository;
import com.xiaoq.users.entity.ThirdAccountInfoEntity;
import com.xiaoq.users.entity.UserEntity;
import com.xiaoq.users.entity.UserPhoneEntity;
import com.xiaoq.users.entity.UserTermDeviceEntity;
import com.xiaoq.users.redis.RedisTemplate4JedisCluster;
import com.xiaoq.users.repository.UserPhoneRepository;
import com.xiaoq.users.repository.UserRepository;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service(interfaceClass=IUserApi.class,version="1.0.0")
public class UserApiImpl implements IUserApi {

	@Autowired
	private UserRepository repo;

	@Autowired
	private IFollowRepository followReposity;
	
	@Autowired
	private RedisTemplate4JedisCluster redisTemplate;

	@Autowired
	private UserPhoneRepository userPhoneRepository;

	@Override
	public UserInfo4SocialVo getUserInfo4Social(String id) {
		UserEntity entity = repo.getById(id);
		if (entity == null) {
			return null;
		}
		UserInfo4SocialVo vo = new UserInfo4SocialVo();
		BeanUtils.copyProperties(entity, vo);
		return vo;
	}

	/**
	 * 根据用户ID获取用户的信息
	 * @param id
	 * @return
	 */
	@Override
	public UserInfo4UserCouponVo getUserInfoById(String id){
		UserInfo4UserCouponVo userInfo4UserCouponVo = null;
		if(StringUtils.isNotBlank(id)){
			UserEntity userEntity = repo.getById(id);
			if(userEntity != null && StringUtils.isNotBlank(userEntity.getId())){
				userInfo4UserCouponVo = new UserInfo4UserCouponVo();
				BeanUtils.copyProperties(userEntity, userInfo4UserCouponVo);

				//是否为匿名登录
				userInfo4UserCouponVo.setAnonymous(false);
				List<UserEntity.Role> roleList = userEntity.getUserRoleList();
				if(CollectionUtils.isNotEmpty(roleList)){
					for(UserEntity.Role role : roleList){
						if(UserEntity.Role.ANONYMOUS.value == role.value){
							userInfo4UserCouponVo.setAnonymous(true);
						}
					}
				}else {
					userInfo4UserCouponVo.setAnonymous(true);
				}

			}
		}
		return userInfo4UserCouponVo;
	}

	@Override
	public List<FollowInfo4InterpreterVo> getFollowList(String userId, List<String> targetIdList, String targetType) {
		List<FollowInfo4InterpreterVo> followInfo4InterpreterVoList = new ArrayList<>();
		List<FollowEntity> followEntityList = followReposity.findFollowListByTargetIdList(userId, targetIdList, targetType);
		if(CollectionUtils.isEmpty(followEntityList)){
			return followInfo4InterpreterVoList;
		}

		followEntityList.forEach(followEntity -> {
			FollowInfo4InterpreterVo followInfo4InterpreterVo = new FollowInfo4InterpreterVo();
			BeanUtils.copyProperties(followEntity, followInfo4InterpreterVo);
			followInfo4InterpreterVoList.add(followInfo4InterpreterVo);
		});

		return followInfo4InterpreterVoList;
	}

	@Override
	public String getShowIdByUserId(String userId) {
		String shopId = redisTemplate.get(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, userId);
		return shopId;
	}

	@Override
	public UserInfo4InterVo getUserByMobile(String mobile) {
		if(StringUtils.isEmpty(mobile)){
			return null;
		}
		UserEntity userEntity = repo.findByMobile(mobile);
		if(userEntity==null || StringUtils.isEmpty(userEntity.getId())){
			return null;
		}

		UserInfo4InterVo userInfo4InterVo = new UserInfo4InterVo();

		BeanUtils.copyProperties(userEntity, userInfo4InterVo);
		List<UserEntity.Role> roleList = userEntity.getUserRoleList();
		if(CollectionUtils.isNotEmpty(roleList)){
			for(UserEntity.Role role : roleList){
				if(UserEntity.Role.ANONYMOUS.value == role.value){
					userInfo4InterVo.setAnonymous(true);
				}
				if(UserEntity.Role.INTERPRETER.value == role.value){
					userInfo4InterVo.setInterpreter(true);
				}
			}
		}else {
			userInfo4InterVo.setAnonymous(true);
			userInfo4InterVo.setInterpreter(false);
		}

		return userInfo4InterVo;
	}

	@Override
	public boolean addInterRole2UserByUserId(String userId) {
		if(StringUtils.isBlank(userId)){
			return false;
		}
		UserEntity userEntity = repo.getById(userId);
		if(userEntity == null){
			return false;
		}

		List<UserEntity.Role> roleList = userEntity.getUserRoleList();

		UserEntity updateUser = new UserEntity();
		updateUser.setId(userId);
		if(CollectionUtils.isEmpty(roleList)){
			updateUser.setUserRoleList(Arrays.asList(UserEntity.Role.INTERPRETER));
		}else if(roleList.indexOf(UserEntity.Role.INTERPRETER) < 0){
			roleList.add(UserEntity.Role.INTERPRETER);
		}
		updateUser.setUserRoleList(roleList);
		return repo.updateUserRole(updateUser);
	}

	@Override
	public boolean removeUserInterRole(String userId) {
		if(StringUtils.isBlank(userId)){
			return false;
		}
		UserEntity userEntity = repo.getById(userId);
		if(userEntity == null){
			return false;
		}

		List<UserEntity.Role> roleList = userEntity.getUserRoleList();
		UserEntity updateUser = new UserEntity();
		updateUser.setId(userId);
		if(CollectionUtils.isEmpty(roleList)){
			updateUser.setUserRoleList(new ArrayList<>());
		}else if(roleList.indexOf(UserEntity.Role.INTERPRETER) > 0){
			roleList.remove(UserEntity.Role.INTERPRETER);
		}
		updateUser.setUserRoleList(roleList);
		return repo.updateUserRole(updateUser);
	}

	@Override
	public List<UserInfo4OrderTourVo> getUserInfo4OrderTourByUserIds(List<String> userIds) {
		List<UserInfo4OrderTourVo> resultList = new ArrayList<>();
		List<UserEntity> userList = repo.findByUserIdList(userIds);
		if(CollectionUtils.isEmpty(userList)){
			return resultList;
		}
		UserInfo4OrderTourVo userInfo4OrderTourVo;
		List<ThirdAccountInfoEntity> thirdAccountList;
		for(UserEntity user : userList){
			userInfo4OrderTourVo = new UserInfo4OrderTourVo();

			userInfo4OrderTourVo.setId(user.getId());
			userInfo4OrderTourVo.setNickName(user.getNickName());
			userInfo4OrderTourVo.setMobile(user.getMobile());

			if(CollectionUtils.isNotEmpty(thirdAccountList = user.getThirdAccountList())){
				for(ThirdAccountInfoEntity thirdAccountInfoEntity : thirdAccountList){
					if("WEIXIN".equalsIgnoreCase(thirdAccountInfoEntity.getThirdAccountType())){
						userInfo4OrderTourVo.setThirdAccountType(thirdAccountInfoEntity.getThirdAccountType());
						userInfo4OrderTourVo.setThirdNickName(thirdAccountInfoEntity.getThirdNickName());
						break;
					}
				}
			}
			UserTermDeviceEntity userTermDeviceEntity;
			if((userTermDeviceEntity = user.getTermDevice()) != null){
				userInfo4OrderTourVo.setOsType(userTermDeviceEntity.getOsType());
				userInfo4OrderTourVo.setMobileMode(userTermDeviceEntity.getMobileMode());
			}

			resultList.add(userInfo4OrderTourVo);
		}
		return resultList;
	}

	@Override
	public List<UserPhone4OrderVo> getUserPhone4OrderTourByUserIds(List<String> userIds) {
		List<UserPhone4OrderVo> resultList = new ArrayList<>();
		List<UserPhoneEntity> userPhoneList = userPhoneRepository.getUserPhoneList(userIds);
		if(CollectionUtils.isEmpty(userPhoneList)){
			return resultList;
		}
		UserPhone4OrderVo userPhone4OrderVo;
		for(UserPhoneEntity userPhoneEntity : userPhoneList){
			userPhone4OrderVo = new UserPhone4OrderVo();
			BeanUtils.copyProperties(userPhoneEntity, userPhone4OrderVo);
			resultList.add(userPhone4OrderVo);
		}

		return resultList;
	}

	@Override
	public List<UserPhone4PositionVo> getUserPhone4PositionByAreaCodeList(List<String> areaCodeList) {
		List<UserPhone4PositionVo> resultList = new ArrayList<>();
		List<UserPhoneEntity> userPhoneEntityList = userPhoneRepository.getUserPhoneListByAreaCodeList(areaCodeList);
		if(CollectionUtils.isNotEmpty(userPhoneEntityList)){
			for (UserPhoneEntity userPhoneEntity : userPhoneEntityList){
				UserPhone4PositionVo userPhone4PositionVo = new UserPhone4PositionVo();
				BeanUtils.copyProperties(userPhoneEntity, userPhone4PositionVo);
				resultList.add(userPhone4PositionVo);
			}
		}
		return resultList;
	}
}
